#
# Script sets bindings up correclty for your Web Application
# 
#
#
## kill Main Install if Terminate = $true
if ($env:terminate -eq $true) {exit 0}

Write-Host -ForegroundColor yellow "Setting up bindings for $env:farmurl"

$appConfig = "$env:systemdrive\Windows\System32\inetsrv\config\applicationHost.config"

[xml]$xmldoc = get-Content $appConfig

$sites = $xmldoc.configuration."system.applicationHost".sites.site
$node = ""
	foreach ($site in $sites){

		if ($site.name -eq $env:farmname){
		#Write-Host -foregroundcolor yellow "Found $env:farmname"
		$node = $site
		}


	}


	#Check if the site has 1 binding

	if ($node.bindings.binding.Length -gt 1){

		$node.removeChild($node.bindings)

		$parentnode = $xmldoc.CreateElement("bindings")
		$node.AppendChild($parentnode)

		$newnode = $xmldoc.CreateElement("binding")
		$newnode.setAttribute("protocol", "http")
		$newnode.setAttribute("bindingInformation", "*:80:")
		$parentnode.AppendChild($newnode)

		$xmldoc.Save($appconfig)
		

	} else
	{

		## Do nothing
	}


	#Check if it has the correct bindings

	if ($node.bindings.binding.bindingInformation -ne "*:80:"){

	# if it is not correct, fix it
	$node.bindings.binding.bindingInformation = "*:80:"
	$xmldoc.Save($appConfig)
	} else {


	## do nothing
	}
	Write-Host -ForegroundColor yellow "Setting up bindings for $env:farmurl Completed."